/**
 * Project Name:HuayuanPay
 * File Name:DeviceUtil.java
 * Package Name:com.sxhuayuan.util
 * Date:2016年6月7日下午4:31:14
 * Copyright (c) 2016, sxhuayuan.com All Rights Reserved.
 *
 */

package com.sxhuayuan.parking.utils;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

/**
 * 检测是否为移动端设备访问
 * 
 * @Version : 1.00
 * @Date : 2014-7-7 下午01:34:31
 */
public class DeviceUtil {

	// \b 是单词边界(连着的两个(字母字符 与 非字母字符) 之间的逻辑上的间隔),
	// 字符串在编译时会被转码一次,所以是 "\\b"
	// \B 是单词内部逻辑间隔(连着的两个字母字符之间的逻辑上的间隔)
	static String phoneReg = "\\b(ip(hone|od)|android|opera m(ob|in)i" + "|windows (phone|ce)|blackberry" + "|s(ymbian|eries60|amsung)|p(laybook|alm|rofile/midp"
			+ "|laystation portable)|nokia|fennec|htc[-_]" + "|mobile|up.browser|[1-4][0-9]{2}x[1-4][0-9]{2})\\b";
	static String tableReg = "\\b(ipad|tablet|(Nexus 7)|up.browser" + "|[1-4][0-9]{2}x[1-4][0-9]{2})\\b";

	// 移动设备正则匹配：手机端、平板
	static Pattern phonePat = Pattern.compile(phoneReg, Pattern.CASE_INSENSITIVE);
	static Pattern tablePat = Pattern.compile(tableReg, Pattern.CASE_INSENSITIVE);
	static Pattern weixinPat = Pattern.compile("MicroMessenger", Pattern.CASE_INSENSITIVE);
	static Pattern alipayPat = Pattern.compile("AlipayClient", Pattern.CASE_INSENSITIVE);

	/**
	 * 检测是否是移动设备访问
	 * 
	 * @Title: check
	 * @Date : 2014-7-7 下午01:29:07
	 * @param userAgent
	 *            浏览器标识
	 * @return true:移动设备接入，false:pc端接入
	 */
	public static boolean isMobile(String userAgent) {
		if (null == userAgent) {
			userAgent = "";
		}
		// 匹配
		Matcher matcherPhone = phonePat.matcher(userAgent);
		Matcher matcherTable = tablePat.matcher(userAgent);
		if (matcherPhone.find() || matcherTable.find()) {
			return true;
		} else {
			return false;
		}
	}

	/** 是否来自微信 */
	public static boolean isFromWeixin(String userAgent) {
		Matcher matcher = weixinPat.matcher(userAgent);
		if (matcher.find()) {
			return true;
		} else {
			return false;
		}
	}

	/** 是否来自支付宝 */
	public static boolean isFromAlipay(String userAgent) {
		Matcher matcher = alipayPat.matcher(userAgent);
		if (matcher.find()) {
			return true;
		} else {
			return false;
		}
	}

	/** 是否来自微信小程序 */
	public static boolean isFromWeinxinProgram(String type) {
		return "wechatApplet".equals(type);
	}

	public static String getRemortIP(HttpServletRequest request) {
		String ip = request.getHeader("x-forwarded-for");
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		return ip.equals("0:0:0:0:0:0:0:1") ? "127.0.0.1" : ip;
	}

	/**
	 * 获取本机的外网ip地址
	 * 
	 * @return
	 */
	public static List<String> getLocalIPList() {
		List<String> ipList = new ArrayList<String>();
		try {
			Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
			NetworkInterface networkInterface;
			Enumeration<InetAddress> inetAddresses;
			InetAddress inetAddress;
			String ip;
			while (networkInterfaces.hasMoreElements()) {
				networkInterface = networkInterfaces.nextElement();
				inetAddresses = networkInterface.getInetAddresses();
				while (inetAddresses.hasMoreElements()) {
					inetAddress = inetAddresses.nextElement();
					if (inetAddress != null && inetAddress instanceof Inet4Address) { // IPV4
						ip = inetAddress.getHostAddress();
						ipList.add(ip);
					}
				}
			}
		} catch (SocketException e) {
			e.printStackTrace();
		}
		return ipList;
	}

	static String ip = null;

	/**
	 * 获取ip
	 * 
	 * @return
	 */
	public static String getInternetIp() {
		if (ip == null) {
			List<String> localIPList = getLocalIPList();
			ip = localIPList.get(localIPList.size() - 1);
		}
		return ip;
	}

	public static void main(String[] args) {
		// List<String> localIPList = getLocalIPList();
		// System.out.println(StringUtils.join(localIPList, ","));
		System.out.println(getInternetIp());
	}
}
